/*
 * Lenovo Group
 * Copyright (c) 2015-2016 All Rights Reserved.
 * Project Name: lmrp-android app
 * Create Time: 16-2-16 下午6:30
 */

package com.lenovo.mso2o.biz;

import com.baidu.location.BDLocation;
import com.lenovo.framework.biz.IMapManager;
import com.lenovo.framework.service.LocationEventsOnSubscribe;
import com.lenovo.framework.util.DLog;

import java.util.concurrent.TimeUnit;

import rx.Observable;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

/**
 * @author JeffreyHe
 * @version 1.0
 * @created 17-一月-2016 14:19:40
 */
public class MapManagerImpl implements IMapManager {
    private Subscription _subscription;
    public static final MapManagerImpl instance = new MapManagerImpl();

	public void startLocationRealTimeSharing(){
        LocationEventsOnSubscribe subscribe = new LocationEventsOnSubscribe();
        subscribe.setScanSpan(5*60*1000);
		_subscription = Observable.create(subscribe)
        .debounce(5, TimeUnit.MINUTES)     // default Scheduler is Computation
        .map(new Func1<BDLocation, Boolean>() {
            @Override
            public Boolean call(BDLocation latLng) {
                //do newwork io
                DLog.d(MapManagerImpl.class.getName(), latLng.toString());
                return true;
            }
        })
        .subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
                //ui thread to handle result
        .subscribe(new Action1<Boolean>() {
            @Override
            public void call(Boolean b) {
                DLog.d(MapManagerImpl.class.getName(), "" + b);
                //do add intervel
            }
        })
        ;
	}

    public void stopLocationRealTimeSharing(){
        if(_subscription != null && !_subscription.isUnsubscribed())
            _subscription.unsubscribe();
    }

	private MapManagerImpl(){
	}

	public void finalize() throws Throwable {
	}

	@Override
	public void initService() {

	}
}